<?php
ls_import("lslib/Error");
ls_import("lslib/Storage");

class MySQL_StorageDriver extends Base_StorageDriver
{
	var $link;
	
	function MySQL_StorageDriver(&$context)
	{
		$link = NULL;
		parent::Base_StorageDriver(&$context);
	}
	
	function connect($server='localhost',$user='root',$pass='',$database='test')
	{
		$this->link = mysql_connect($server,$user,$pass);
		$this->selectDatabase($database);
	}
	
	function execute(&$sql)
	{
		if(is_object($sql) && is_subclass_of($sql,'SqlQueryBuilder')){
			$sql = $sql->buildQuery();
		}
		if(strlen($sql)<1){
			return new StorageError("Query Empty");	
		}
		$rs = mysql_query($sql,$this->link);
		if($rs === false){
			return new StorageError(mysql_error(),mysql_errno());
		}
		return new MySQL_StorageDriver_Result($rs);
	}
	
	function query(&$sql)
	{
		if(is_object($sql) && is_subclass_of($sql,'SqlQueryBuilder')){
			$sql = $sql->buildQuery();
		}
		if(strlen($sql)<1){
			return new StorageError("Query Empty");	
		}
		$rs = mysql_query($sql,$this->link);
		if($rs === false){
			return new StorageError(mysql_error(),mysql_errno());
		}
		return new MySQL_StorageDriver_RecordSet($rs);
		
	}
}
class MySQL_StorageDriver_Result
{
	var $recordSet;
	
	function MySQL_StorageDriver_Result(&$rs,&$link){
		$this->recordSet = &$rs;
		$this->affected_rows = mysql_affected_rows(&$this->link);
	}
	
	function affectedRows(){
		return $this->affected_rows;
	}
	
	
	
}
class MySQL_StorageDriver_RecordSet extends Base_StorageDriver_RecordSet
{
	var $RecordSet;

	function MySQLStorageProvider_RecordSet(&$rs){
		$this->RecordSet = &$rs;
		$this->Field = array();
		$this->Count = 0;

		$this->Row = array();
		$this->RowIndex = 0;
		$this->FetchIndex = 0;

		if($rs){
			$this->Field = mysql_fetch_field($rs);
			$this->Count = mysql_num_rows($rs);
		}
	}

	function rowCount(){
		return $this->Count;
	}

	function free(){
		return @mysql_free_result($this->rs);
	}

	function getRowByIndex($index = 1){
		if($index >0 && $index <=$this->Count){
			if($this->FetchIndex+1 == $index ){
				$this->FetchIndex ++;
				$row = @mysql_fetch_assoc($this->recordSet) ;
				if(!$row){
				return new StorageError(mysql_error(),mysql_errno());
				$this->Row[] = $row;
			}
			return $this->Row[$index-1];
		}
		return array();
	}

	function rowsToArray(){
		if($this->Count < 1)
		return array();
		while($this->FetchIndex<$this->Count && $this->Count >0){
			$this->next();
		}
		return $this->Row;
	}

	function hasPrevious(){
		return ($this->RowIndex+1 > 1 && $this->Count>0)?true:false;
	}

	function hasNext(){
		return ($this->RowIndex < $this->Count && $this->Count>0)?true:false;
	}

	function previous(){
		$returnAry = array();
		if($this->hasPrevious()){
			$returnAry = $this->getRowByIndex($this->RowIndex);
			$this->RowIndex --;
			return $returnAry;
		}
		return new StorageError(mysql_error(),mysql_errno());
	}

	function next(){
		$returnAry = array();
		if($this->hasNext()){
			$returnAry = $this->getRowByIndex($this->RowIndex+1);
			$this->RowIndex ++;
			return $returnAry;
		}
		return new StorageError(mysql_error(),mysql_errno());
	}

	function fetchOne(){
		return $this->Row[$this->RowIndex][0];
	}

	function fetchRow(){
		$ary = $this->next();
		return $ary;
	}

	function fetchAll(){
		return $this->rowsToArray();
	}

	function fetchCol(){
		return $this->Field;
	}

	function numCols(){
		return count($this->Fetch);
	}

	function numRows(){
		return $this->Count;
	}

	function seek($rowNum=0){
		return $this->getRowByIndex($rowNum);
	}

	function reset($startIndex = 1){
		if($startIndex <1 )$this->RowIndex = 0;
		if($startIndex > $this->Count)$this->RowIndex = $this->Count-1;
		$this->RowIndex = $startIndex -1;
	}
}

	function rowsToArray(){
		if($this->Count < 1)
		return array();
		while($this->FetchIndex<$this->Count && $this->Count >0){
			$this->next();
		}
		return $this->Row;
	}

	function next(){
		if($this->hasNext()){
			parent::next();
			return $this->getRowByIndex($this->RowIndex);
		}
		return new Error("Index Out Of Box");
	}
	
	function previous(){
		if($this->hasPrevious()){
			parent::previous();
			return $this->getRowByIndex($this->RowIndex);
		}
		return new Error("Index Out Of Box");
	}
	
	function fetchOne(){
		return $this->Row[$this->RowIndex][0];
	}

	function fetchRow(){
		$ary = $this->next();
		return $ary;
	}

	function fetchAll(){
		return $this->rowsToArray();
	}

	function getField(){
		return $this->Field;
	}

	function fieldCount(){
		return count($this->Field);
	}

	function seek($rowNum = 0){
		return $this->getRowByIndex($rowNum);
	}

	function reset($startIndex = 1){
		if($startIndex <1 )$this->RowIndex = 0;
		if($startIndex > $this->Count)$this->RowIndex = $this->Count-1;
		$this->RowIndex = $startIndex -1;
	}
	
}



?>